#include <iostream>
#include <pthread.h>
#include <unistd.h>

class Task
{
public:
	Task(int a, int b) : _a(a), _b(b) {}
	~Task() = default;

	int add() const {
		return _a + _b;
	}

private:
	int _a;
	int _b;
};

class Show{
public:
	Show() = default;
	~Show() = default;
	void display(const Task &task) {
		std::cout << "Task result: " << task.add() << std::endl;
	}
private:

};

void *routine(void *arg)
{
	std::string str = static_cast<const char *>(arg);
	Task* t = new Task(10, 20);
	Show show;
	int cnt = 8;
	while (cnt--)
	{
		std::cout << "新线程正在运行: " << str << std::endl;
		show.display(*t);
		sleep(1);
	}

	delete t;
	return (void *)10;
}

int main()
{
	pthread_t tid;
	pthread_create(&tid, nullptr, routine, (void*)"create a new thread");

	int num = 5;
	while (num--)
	{
		std::cout << "主线程" << std::endl;
		sleep(1);
	}

	std::cout << "主线程执行完毕，等待新线程结束" << std::endl;
	void *ret = nullptr;
	pthread_join(tid, &ret);

	return 0;
}